<?php

	function getPipePos($n, $line)
	{
		$pos = 0;
		for ($i = 0; $i < $n; $i++)
		{
			$pos = strpos($line, "|", $pos + 1);
		}
		return $pos;
	}

	function getRank($line)
	{
		$length = getPipePos(1, $line);
		return substr($line, 0, $length);
	}

	function getField($s, $e, $line)
	{
		$start = getPipePos($s, $line) + 1;
		$length = getPipePos($e, $line) - $start;
		return substr($line, $start, $length);
	}

	function getStats($line)
	{
		//pV|3V|rV|aV|sV|bV|fg%V|ft%V|toV
		$pts = getField(2, 3, $line);
		$pm3 = getField(3, 4, $line);
		$reb = getField(4, 5, $line);
		$ast = getField(5, 6, $line);
		$stl = getField(6, 7, $line);
		$blk = getField(7, 8, $line);
		$fgp = getField(8, 9, $line);
		$ftp = getField(9, 10, $line);
		$to = getField(10, 11, $line);

		//aFG|aFT|a3PTM|aPTS|aREB|aAST|aST|aBLK|aTO
		return $fgp."|".$ftp."|".$pm3."|".$pts."|".$reb."|".$ast."|".$stl."|".$blk."|".$to."|";
	}

	$zbbmfilename = "zscores_bbm_lastseasonstats.txt";
	$zbbmfile = file_get_contents("raw/" . $zbbmfilename);
	$zbbmlines = explode("\n", $zbbmfile);

	$zfbnfilename = "zscores_fbninja_projected.txt";
	$zfbnfile = file_get_contents("raw/" . $zfbnfilename);
	$zfbnlines = explode("\n", $zfbnfile);

	$overallfilename = "espnyahoorazz.data";
	$overallfile = file_get_contents("out/" . $overallfilename);
	$overalllines = explode("\n", $overallfile);

	$AGGREGATED_LIST = "Y!|E|RZ|zlVAL|zpVAL|NAME|TEAM|POS|GMS|MIN|".
						"pFGM|pFGA|pFG%|pFTM|pFTA|pFT%|p3PTM|pPTS|pREB|pAST|pST|pBLK|pTO|".
						"lFGM|lFGA|lFG%|lFTM|lFTA|lFT%|l3PTM|lPTS|lREB|lAST|lST|lBLK|lTO|".
						"a3PTM|aPTS|aREB|aAST|aST|aBLK|aTO|".
						"zlFG|zlFT|zl3PTM|zlPTS|zlREB|zlAST|zlST|zlBLK|zlTO|".
						"zpFG|zpFT|zp3PTM|zpPTS|zpREB|zpAST|zpST|zpBLK|zpTO|";
	$i = 0;

	$NOT_BBM = "Not in BBM";
	$NOT_BBM_C = 0;
	$NOT_FBN = "Not in FBNinja";
	$NOT_FBN_C = 0;
	foreach($overalllines as $overallline)
	{
		if ($i > 0)
		{
			$name = getField(3, 4, $overallline);

			$bbmStats = "0|0|0|0|0|0|0|0|0|";
			$bbmVal = "0";
			foreach($zbbmlines as $zbbmline)
			{
				$pos = strpos($zbbmline, $name);
				if ($pos !== false)
				{
					$bbmStats = getStats($zbbmline);
					$bbmVal = getRank($zbbmline);
					break;
				}
			}

			$fbnStats = "0|0|0|0|0|0|0|0|0|";
			$fbnVal = "0";
			foreach($zfbnlines as $zfbnline)
			{
				$pos = strpos($zfbnline, $name);
				if ($pos !== false)
				{
					$fbnStats = getStats($zfbnline);
					$fbnVal = getRank($zfbnline);
					break;
				}
			}
			$playerdata = $overallline."|".$bbmStats.$fbnStats;
			$playerdata = str_replace("--|", "0|", $playerdata);
			$AGGREGATED_LIST .= "\n". 
				substr_replace($playerdata, "|".$bbmVal."|".$fbnVal, getPipePos(3, $playerdata), 0);
		}
		$i++;
	}
	if (file_put_contents("out/fullplayerstats.data", $AGGREGATED_LIST) !== false)
	{	
		echo "<h3>Successfully aggregated zscores to out/$overallfilename.</h3>" .
			"<h3>Processed data is at out/fullplayerstats.data.</h3>";
		echo "<iframe src='out/fullplayerstats.data' width='100%' height='80%'>";
	}
	else
	{
		echo "<h3>Error processing raw data.</h3>";
	}
?>